<?php

class Nuo_View_Helper_Locale extends Zend_View_Helper_Abstract
{

	/**
	 * locale instance
	 * @property Zend_Locale
	 */
	protected $_locale;

	/**
	 * magic call from view helper
	 * @return Nuo_View_Helper_Locale
	 */
	public function locale()
	{
		return $this;
	}

	/**
	 * format number with locale
	 * @param number $number
	 * @param array $options
	 * @return string
	 */
	public function toNumber($number, $options = array())
	{
		$number = Zend_Locale_Format::toNumber($number, $options);
		return $number;
	}

	public function toDate($date, $options = array())
	{
		$options = array_merge(array(
			'locale' => $this -> getLocale(),
			'size' => 'short',
			'type' => 'date',
			'timezone' => Zend_Registry::get('timezone'),
		), $options);

		$date = $this -> _checkDateTime($date, $options);
		if (!$date)
		{
			return false;
		}

		if (empty($options['format']))
		{
			$options['format'] = Zend_Locale_Data::getContent($options['locale'], $options['type'], $options['size']);
		}
		// Hack for weird usage of L instead of M in Zend_Locale
		$options['format'] = str_replace('L', 'M', $options['format']);

		$str = $date -> toString($options['format'], $options['locale']);
		$str = $this -> convertNumerals($str, $options['locale']);
		return $str;
	}

	/**
	 * format date time object
	 * @param string $date date time string
	 * @return string
	 */
	public function toDatetime($date, $options = array())
	{
		$options = array_merge(array(
			'locale' => $this -> getLocale(),
			'size' => 'long',
			'type' => 'datetime',
			'timezone' => Zend_Registry::get('timezone'),
		), $options);

		$date = $this -> _checkDateTime($date, $options);

		if (!$date)
		{
			return '';
		}

		if (empty($options['format']))
		{
			$options['format'] = Zend_Locale_Data::getContent($options['locale'], $options['type'], $options['size']);
		}
		// Hack for weird usage of L instead of M in Zend_Locale
		$options['format'] = str_replace('L', 'M', $options['format']);

		$str = $date -> toString($options['format'], $options['locale']);

		$str = $this -> convertNumerals($str, $options['locale']);

		return $str;

	}

	/**
	 * format currency
	 * @param number $number
	 * @return string
	 */
	public function toCurrency($number, $currency = null, $options)
	{
		return $number;
	}

	protected function _checkDateTime($datetime, $options = array())
	{
		if (is_numeric($datetime))
		{
			$datetime = new Zend_Date($datetime);
		}
		else
		if (is_string($datetime))
		{
			$datetime = new Zend_Date(strtotime($datetime));
		}
		else
		if (!($datetime instanceof Zend_Date))
		{
			return false;
		}

		if (!($datetime instanceof Zend_Date))
		{
			throw new Engine_Exception('Not a valid date');
		}

		if (!isset($options['timezone']) && Zend_Registry::isRegistered('timezone'))
		{
			$options['timezone'] = Zend_Registry::get('timezone');
		}

		if (!isset($options['locale']))
		{
			$options['locale'] = $this -> getLocale();
		}

		if ($options['timezone'])
		{
			$datetime -> setTimezone($options['timezone']);
		}

		if ($options['locale'])
		{
			$datetime -> setLocale($options['locale']);
		}

		return $datetime;
	}

	public function convertNumerals($string, $locale = null)
	{
		if (!$locale)
		{
			$locale = $this -> getLocale();
		}

		// Get the numbering system
		$defaultNumberingSystem = null;
		try
		{
			$defaultNumberingSystem = Zend_Locale_Data::getContent($locale, 'defaultnumberingsystem');
		}
		catch( Zend_Locale_Exception $e )
		{
			// Silence
		}

		// Convert now
		if ($defaultNumberingSystem && 'latn' != strtolower($defaultNumberingSystem))
		{
			$string = Zend_Locale_Format::convertNumerals($string, 'Latn', $defaultNumberingSystem);
		}

		return $string;
	}

	/**
	 * Set the current locale
	 *
	 * @param string|Zend_Locale $locale
	 * @return Engine_View_Helper_Locale
	 */
	public function setLocale($locale)
	{
		if (is_string($locale))
		{
			$locale = new Zend_Locale($locale);
		}

		if (!$locale instanceof Zend_Locale)
		{
			throw new Zend_View_Exception('Not passed locale object or valid locale string');
		}

		$this -> _locale = $locale;
		return $this;
	}

	/**
	 * Get the current locale. Defaults to locale in registry
	 *
	 * @return Zend_Locale
	 */
	public function getLocale()
	{
		if (null === $this -> _locale)
		{
			$this -> _locale = Zend_Registry::get('Locale');
		}

		return $this -> _locale;
	}

	public function getTimezone()
	{
		return Zend_Registry::get('timezone');
	}

}
